﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Web4Comment.Service
{
    //class FriendsService:IFriendsService
    public static class FriendsService
    {
        private static UnlessComment.BLL.Friend friends = new UnlessComment.BLL.Friend();
        private static UnlessComment.BLL.User users = new UnlessComment.BLL.User();
        //private static UnlessComment.BLL.UserInfo userInfos = new UnlessComment.BLL.UserInfo();

        public static int addFriends(string host_username, string friend_username)
        {
            int hostId = Service.UserService.getUserid(host_username);
            int friendId = Service.UserService.getUserid(friend_username);
            
            
            if (friends.GetModelList("host_Id = " + hostId + " and friend_Id = " + friendId).Count > 0)
                return -1;
            UnlessComment.Model.Friend friend = new UnlessComment.Model.Friend();
            friend.host_id = hostId;
            friend.friend_id = friendId;
            friend.date = System.DateTime.Now;
            return friends.Add(friend);
        }

        public static int deleteFriends(string host_username, string friend_username)
        {
            int hostId = Service.UserService.getUserid(host_username);
            int friendId = Service.UserService.getUserid(friend_username);
            List<UnlessComment.Model.Friend> friendList = friends.GetModelList("host_Id = " + hostId + " and friend_Id = " + friendId);
            for (int i = 0; i < friendList.Count; i++)
            {
                if (friends.Delete(friendList[i].id))
                    return 1;
            }
            return 0;
        }

        public static List<UnlessComment.Model.FriendDetail> getMyFriends(string username)
        {
            List<UnlessComment.Model.FriendDetail> friendDetailList = new List<UnlessComment.Model.FriendDetail>();
            List<UnlessComment.Model.Friend> friendList = new List<UnlessComment.Model.Friend>();
            UnlessComment.Model.FriendDetail friendDetail;
            int user_id = UserService.getUserid(username);
            friendList = friends.GetModelList("host_id = " + user_id);
            for (int i = 0; i < friendList.Count; i++)
            {
                friendDetail = new UnlessComment.Model.FriendDetail(friendList[i]);
                friendDetail.friend_username = users.GetModel(friendDetail.friend_id.Value).username;
                friendDetail.host_username = users.GetModel(friendDetail.host_id.Value).username;
                friendDetailList.Add(friendDetail);
            }
            return friendDetailList;
        }

        public static List<UnlessComment.Model.UserTotal> searchFriends(Object property,string host_username)
        {
            List<UnlessComment.Model.UserTotal> userTotalList = new List<UnlessComment.Model.UserTotal>();
            List<UnlessComment.Model.User> userList = new List<UnlessComment.Model.User>();
            userList = users.GetModelList("username like '%" + (String)property + "%'");
            for (int i = 0; i < userList.Count; i++)
            {
                if (checkIsFriend(host_username, userList[i].username) && userList[i].username != host_username)
                {
                    UnlessComment.Model.UserTotal temp = new UnlessComment.Model.UserTotal();
                    temp = Service.UserService.getUserTotal(userList[i].username);
                    userTotalList.Add(temp);
                }
            }
            return userTotalList;
        }

        public static List<UnlessComment.Model.UserTotal> searchNotFriends(Object property, string host_username)
        {
            List<UnlessComment.Model.UserTotal> userTotalList = new List<UnlessComment.Model.UserTotal>();
            List<UnlessComment.Model.User> userList = new List<UnlessComment.Model.User>();
            userList = users.GetModelList("username like '%" + (String)property + "%'");
            for (int i = 0; i < userList.Count; i++)
            {
                if (!checkIsFriend(host_username, userList[i].username) && userList[i].username != host_username)
                {
                    UnlessComment.Model.UserTotal temp = new UnlessComment.Model.UserTotal();
                    temp = Service.UserService.getUserTotal(userList[i].username);
                    userTotalList.Add(temp);
                }
            }
            return userTotalList;
        }

        public static UnlessComment.Model.User getSingleFriend(string friend_username)
        {
            int friendId = Service.UserService.getUserid(friend_username);
            return users.GetModel(friendId);
        }

        public static bool checkIsFriend(string host_name, string friend_username)
        {
            int host_id = Service.UserService.getUserid(host_name);
            int friend_id = Service.UserService.getUserid(friend_username);
            if (friends.GetModelList("host_id = " + host_id + " and friend_id = " + friend_id).Count == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool modifyFriend(string host_name, string friend_username, bool school, bool company, bool address, bool hobby)
        {
            int host_id = Service.UserService.getUserid(host_name);
            int friend_id = Service.UserService.getUserid(friend_username);
            List<UnlessComment.Model.Friend> friendList =  friends.GetModelList("host_id = " + host_id + " and friend_id = " + friend_id);
            if (friendList.Count == 0)
            {
                return false;
            }
            else
            {
                UnlessComment.Model.Friend friendModel = friendList[0];
                friendModel.by_address = address;
                friendModel.by_company = company;
                friendModel.by_hobby = hobby;
                friendModel.by_school = school;
                return friends.Update(friendModel);
            }
        }

        public static UnlessComment.Model.Friend getFriendType(string host_name, string friend_username)
        {
            int host_id = Service.UserService.getUserid(host_name);
            int friend_id = Service.UserService.getUserid(friend_username);
            List<UnlessComment.Model.Friend> friendList = friends.GetModelList("host_id = " + host_id + " and friend_id = " + friend_id);
            if (friendList.Count == 0)
            {
                return null;
            }
            else
            {
                return friendList[0];
            }
        }
    }
}
